package yxy.game.pm2.command.kow;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.KowMessage;
import yxy.game.pm2.module.kow.KowHandle;

/** 天下第一 天下第一挑战记录 */
@MessageType(proto = Command.KowRecord, classtype = KowMessage.KowRecordRequest.class)
public class CMD_KowRecord extends Command<KowMessage.KowRecordRequest> {

	@Override
	public void execute(MessageSource messageSource, KowMessage.KowRecordRequest request) {
		String playerCode = messageSource.getTag();

		KowMessage.KowRecordResponse.Builder response = KowMessage.KowRecordResponse.newBuilder();

		try {
			Result result = new KowHandle().record(playerCode, response);
			send(messageSource, response, result);
		} catch (Exception e) {
			e.printStackTrace();
			send(messageSource, response, Result.DataError);
		}

		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, KowMessage.KowRecordResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}